package com.ptswitch.material.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.service.ApplicationStateService;

@Service
public class ApplicationStateServiceImpl implements ApplicationStateService {

    private final Map<String, Uzer> loginUser = new HashMap<String, Uzer>();

    public Uzer findUzer(Uzer user) {
        if (Uzer.Username_Guess.equals(user.getUsername())) {
            return user;
        }
        Uzer cache = loginUser.get(user.getUsername());
        if (cache != null && cache.getPassword().equals(user.getPassword())) {
            return cache;
        }
        return null;
    }

    public void cacheLoginUser(Uzer user) {
        loginUser.put(user.getUsername(), user);
    }

    public void logout(Uzer user) {
        if (user == null) {
            return;
        }
        loginUser.remove(user.getUsername());
    }

}
